package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserAccountDetailMapper userAccountDetailMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertUserAccount(Map map) {
        UserAccount userAccount = new UserAccount();
        Long userId = (Long) map.get("userId");
        String title = (String) map.get("title");
        BigDecimal account = (BigDecimal) map.get("amount");
        String orderNo = (String) map.get("orderNo");
        userAccount.setUserId(userId);
        userAccount.setTotalAmount(account);
        userAccount.setAvailableAmount(account);
        userAccount.setLockAmount(BigDecimal.ZERO);
        userAccount.setTotalIncomeAmount(BigDecimal.ZERO);
        int num = userAccountMapper.insert(userAccount);
        if (num > 0) {
            if (userAccount.getTotalAmount().compareTo(BigDecimal.ZERO) == 1) {
                this.updateUserAccount(userId, title, account, orderNo);
            }
        }
        //进行关联标的查询 设置他的日志
    }

    @Override
    public void updateUserAccount(Long userId, String title, BigDecimal account, String orderNo) {
        UserAccountDetail userAccountDetail = new UserAccountDetail();
        userAccountDetail.setUserId(userId);
        userAccountDetail.setTitle(title);
        userAccountDetail.setTradeType("1201");
        userAccountDetail.setAmount(account);
        userAccountDetail.setOrderNo(orderNo);
        userAccountDetailMapper.insert(userAccountDetail);
    }

    @Override
    public BigDecimal getAvailableAmount(Long userId) {
        UserAccount userAccount = userAccountMapper.selectOne(
                new LambdaQueryWrapper<UserAccount>()
                        .eq(UserAccount::getUserId, userId)
                        .select(UserAccount::getAvailableAmount));
        if (userAccount != null) {
            return userAccount.getAvailableAmount();
        }
        return new BigDecimal("0.00");
    }

    /**
     * 用户账户的扣款
     *
     * @param accountDeductVo
     */
    @Override
    public void checkAndDeduct(AccountDeductVo accountDeductVo) {
        //1.扣除用户的账户
        int row = userAccountMapper.checkAndDeduct(accountDeductVo);
        //1.1 判断是否更新成功
        if (row == 0) {
            throw new GuiguException(500, "账户扣除失败");
        }
        //2.更新用户的账户的日志
        this.updateUserAccount(accountDeductVo.getUserId(), accountDeductVo.getContent(), accountDeductVo.getAmount(), accountDeductVo.getOrderNo());
    }
}
